package cn.bonoon.services.plugins;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bonoon.core.plugins.HolidayService;
import cn.bonoon.entities.calendars.HolidayEntity;
import cn.bonoon.kernel.support.services.AbstractService;

@Service
@Transactional(readOnly = true)
public class HolidayServiceImpl extends AbstractService<HolidayEntity> implements HolidayService {

	@Override
	public HolidayEntity getYear(int year) {
		return __first(HolidayEntity.class, "from HolidayEntity x where x.year=?", year);
	}

	@Override
	public String getMonth(int year, int month) {
		//String[] months = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" };
		return __first(String.class, "select x." + months[month] + " from HolidayEntity x where x.year=?", year);
	}
	
	private final String[] months = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" };

	@Override
	public Object get2Month(int year, int month_1, int month_2) {
		Query query = entityManager.createQuery("select x." + months[month_1] + ", x." + months[month_2] + " from HolidayEntity x where x.year=?");
		List<?> items = query.setParameter(1, year).getResultList();
		return items.isEmpty() ? null : items.get(0);
	}

}
